using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace QuestFramework
{
    public class Input
    {
        private static Input instance = null;
        private static Object classLock = typeof(Input);
        private GamePadState currentP1State;
        private GamePadState previousP1State;
        private GamePadState currentP2State;
        private GamePadState previousP2State;
        private GamePadState currentP3State;
        private GamePadState previousP3State;
        private GamePadState currentP4State;
        private GamePadState previousP4State;
        private KeyboardState currentKeyboardState;
        private KeyboardState previousKeyboardState;
#if !XBOX360
        private MouseState currentMouseState;
        private MouseState previousMouseState;
#endif
        
        private Input()
        {
        }

        public static Input Instance
        {
            get
            {
                lock (classLock)
                {
                    if (instance == null)
                    {
                        instance = new Input();
                    }

                    return instance;
                }
            }
        }

        public void Initialize()
        {
        }

        public void Update()
        {
            previousP1State = currentP1State;
            previousP2State = currentP2State;
            previousP3State = currentP3State;
            previousP4State = currentP4State;
            previousKeyboardState = currentKeyboardState;
#if !XBOX360
            previousMouseState = currentMouseState;
#endif
            currentP1State = GamePad.GetState(PlayerIndex.One);
            currentP2State = GamePad.GetState(PlayerIndex.Two);
            currentP3State = GamePad.GetState(PlayerIndex.Three);
            currentP4State = GamePad.GetState(PlayerIndex.Four);
            currentKeyboardState = Keyboard.GetState();
#if !XBOX360
            currentMouseState = Mouse.GetState();
#endif
        }

        public GamePadState CurrentP1State
        {
            get
            {
                return currentP1State;
            }
        }

        public GamePadState PreviousP1State
        {
            get
            {
                return previousP1State;
            }
        }

        public GamePadState CurrentP2State
        {
            get
            {
                return currentP2State;
            }
        }

        public GamePadState PreviousP2State
        {
            get
            {
                return previousP2State;
            }
        }

        public GamePadState CurrentP3State
        {
            get
            {
                return currentP3State;
            }
        }

        public GamePadState PreviousP3State
        {
            get
            {
                return previousP3State;
            }
        }

        public GamePadState CurrentP4State
        {
            get
            {
                return currentP4State;
            }
        }

        public GamePadState PreviousP4State
        {
            get
            {
                return previousP4State;
            }
        }

        public KeyboardState CurrentKeyboardState
        {
            get
            {
                return currentKeyboardState;
            }
        }

        public KeyboardState PreviousKeyboardState
        {
            get
            {
                return previousKeyboardState;
            }
        }

#if !XBOX360
        public MouseState CurrentMouseState
        {
            get
            {
                return currentMouseState;
            }
        }

        public MouseState PreviousMouseState
        {
            get
            {
                return previousMouseState;
            }
        }
#endif

        public GamePadState GetCurrentGamePadState(PlayerIndex playerIndex)
        {
            switch (playerIndex)
            {
                case PlayerIndex.One:
                    {
                        return currentP1State;
                    }
                case PlayerIndex.Two:
                    {
                        return currentP2State;
                    }
                case PlayerIndex.Three:
                    {
                        return currentP3State;
                    }
                case PlayerIndex.Four:
                    {
                        return currentP4State;
                    }
                default: //Otherwise return player 1 state
                    {
                        return currentP1State;
                    }
            }
        }

        public GamePadState GetPreviousGamePadState(PlayerIndex playerIndex)
        {
            switch (playerIndex)
            {
                case PlayerIndex.One:
                    {
                        return previousP1State;
                    }
                case PlayerIndex.Two:
                    {
                        return previousP2State;
                    }
                case PlayerIndex.Three:
                    {
                        return previousP3State;
                    }
                case PlayerIndex.Four:
                    {
                        return previousP4State;
                    }
                default: //Otherwise return player 1 state
                    {
                        return previousP1State;
                    }
            }
        }

        public bool IsStartPressedOnAnyPad()
        {
            if ((currentP1State.Buttons.Start == ButtonState.Pressed &&
                previousP1State.Buttons.Start != ButtonState.Pressed) ||
                (currentP2State.Buttons.Start == ButtonState.Pressed &&
                previousP2State.Buttons.Start != ButtonState.Pressed) ||
                (currentP3State.Buttons.Start == ButtonState.Pressed &&
                previousP3State.Buttons.Start != ButtonState.Pressed) ||
                (currentP4State.Buttons.Start == ButtonState.Pressed &&
                previousP4State.Buttons.Start != ButtonState.Pressed))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool IsDPadUpPressedOnAnyPad()
        {
            if ((currentP1State.DPad.Up == ButtonState.Pressed &&
                previousP1State.DPad.Up != ButtonState.Pressed) ||
                (currentP2State.DPad.Up == ButtonState.Pressed &&
                previousP2State.DPad.Up != ButtonState.Pressed) ||
                (currentP3State.DPad.Up == ButtonState.Pressed &&
                previousP3State.DPad.Up != ButtonState.Pressed) ||
                (currentP4State.DPad.Up == ButtonState.Pressed &&
                previousP4State.DPad.Up != ButtonState.Pressed) ||
                (currentP1State.ThumbSticks.Left.Y > 0 &&
                previousP1State.ThumbSticks.Left.Y == 0) ||
                (currentP2State.ThumbSticks.Left.Y > 0 &&
                previousP2State.ThumbSticks.Left.Y == 0) ||
                (currentP3State.ThumbSticks.Left.Y > 0 &&
                previousP3State.ThumbSticks.Left.Y == 0) ||
                (currentP4State.ThumbSticks.Left.Y > 0 &&
                previousP4State.ThumbSticks.Left.Y == 0))

            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool IsDPadDownPressedOnAnyPad()
        {
            if ((currentP1State.DPad.Down == ButtonState.Pressed &&
                previousP1State.DPad.Down != ButtonState.Pressed) ||
                (currentP2State.DPad.Down == ButtonState.Pressed &&
                previousP2State.DPad.Down != ButtonState.Pressed) ||
                (currentP3State.DPad.Down == ButtonState.Pressed &&
                previousP3State.DPad.Down != ButtonState.Pressed) ||
                (currentP4State.DPad.Down == ButtonState.Pressed &&
                previousP4State.DPad.Down != ButtonState.Pressed) ||
                (currentP1State.ThumbSticks.Left.Y < 0 &&
                previousP1State.ThumbSticks.Left.Y == 0) ||
                (currentP2State.ThumbSticks.Left.Y < 0 &&
                previousP2State.ThumbSticks.Left.Y == 0) ||
                (currentP3State.ThumbSticks.Left.Y < 0 &&
                previousP3State.ThumbSticks.Left.Y == 0) ||
                (currentP4State.ThumbSticks.Left.Y < 0 &&
                previousP4State.ThumbSticks.Left.Y == 0))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool IsAnyAcceptPressedOnAnyPad()
        {
            if ((currentP1State.Buttons.A == ButtonState.Pressed &&
                previousP1State.Buttons.A != ButtonState.Pressed) ||
                (currentP2State.Buttons.A == ButtonState.Pressed &&
                previousP2State.Buttons.A != ButtonState.Pressed) ||
                (currentP3State.Buttons.A == ButtonState.Pressed &&
                previousP3State.Buttons.A != ButtonState.Pressed) ||
                (currentP4State.Buttons.A == ButtonState.Pressed &&
                previousP4State.Buttons.A != ButtonState.Pressed) ||
                IsStartPressedOnAnyPad())
            {
                return true;
            }
            else
            {
                return false;
            }
        }

    }
}
